<template>
  <div class="app-container">
    <u-page title="短信日志">

      <u-filtered>

        <el-form :inline="true" :model="listQuery" class="search-form">
          <el-form-item label="模板类型" prop="type">
            <el-select v-model="listQuery.type" placeholder="模板类型" clearable>
              <el-option label="全部" value="" />
              <el-option label="通知" :value="1" />
              <el-option label="验证码" :value="2" />
            </el-select>
          </el-form-item>
          <el-form-item label="手机号">
            <el-input v-model="listQuery.mobile" placeholder="手机号" clearable />
          </el-form-item>
          <el-form-item label="模板ID" prop="templateId">
            <el-input v-model="listQuery.templateId" placeholder="模板ID" clearable />
          </el-form-item>
          <el-form-item label="发送状态" class="ack">
            <el-select v-model="listQuery.ack" placeholder="发送状态" clearable>
              <el-option label="全部" value="" />
              <el-option label="成功" value="1" />
              <el-option label="失败" value="2" />
              <el-option label="待发送" value="0" />
            </el-select>
          </el-form-item>
          <el-form-item label="发送时间">
            <u-date-time-picker v-model="searchDate" value-format="yyyy-MM-dd HH:mm:ss" @change="changeSearchDate" />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="hdlFilter">查询</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-download" @click="handleDownload">导出</el-button>
          </el-form-item>
        </el-form>

      </u-filtered>

      <u-table
        :data="list"
        :loading="listLoading"
        :options="listOptions"
        :columns="columns"
        :pagination.sync="listQuery"
        :fetch="hdlList"
      />

    </u-page>

  </div>
</template>
<script>

export default {
  data: function() {
    return {
      api: this.$u.api.PluginsSmsLog,
      // 表格
      columns: [
        {
          prop: 'mobile',
          label: '手机号'
        },
        {
          prop: 'createDate',
          label: '发送时间'
        },
        {
          prop: 'type',
          label: '模板类型',
          render: (h, params) => {
            const { type } = params.row
            const title = type === 1 ? '通知' : '验证码'
            return h('el-tag', title)
          }
        },
        {
          prop: 'templateId',
          label: '模板ID'
        },
        {
          prop: 'content',
          label: '短信内容'
        },
        {
          prop: 'ack',
          label: '发送状态',
          render: (h, params) => {
            if (params.row.ack === 1) {
              return h('el-tag', { props: { type: 'success' }}, '成功')
            } else if (params.row.ack === 2) {
              return h('el-tag', { props: { type: 'danger' }}, '失败')
            } else {
              return h('el-tag', { props: { type: 'warning' }}, '待发送')
            }
          }
        },
        {
          prop: 'createdAt',
          label: '创建时间',
          timestamp: true
        },
        {
          prop: 'note',
          label: '备注'
        }
      ],

      list: [],
      listLoading: true,
      listOptions: {
        stripe: true // boolean 斑马纹
      },
      listQuery: {
        pageNumber: 1,
        pageSize: 10,
        totalCount: 1,
        beginTime: '',
        endTime: '',
        ack: ''
      },
      dialogFormVisible: false,
      dataForm: {}
    }
  },
  created() {
    this.hdlList()
  },
  methods: {
    // 导出数据
    handleDownload() {
      // 复制值
      const list = this.$u.deepClone(this.list)
      // 业务数据处理
      list.forEach(item => {
        this.$set(item, 'type', item.type === 1 ? '通知' : '验证码')
        this.$set(item, 'ack', item.ack === 0 ? '待发送' : item.ack === 1 ? '成功' : item.ack === 2 ? '失败' : '')
        this.$set(item, 'createdAt', item.createdAt ? this.$u.timeFormat(item.createdAt) : '')
      })

      // 表头&表数据
      const tHeader = ['手机号', '发送时间', '模板类型', '模板ID', '短信内容', '状态', '创建时间', '备注']
      const filterVal = ['mobile', 'createDate', 'type', 'templateId', 'content', 'ack', 'createdAt', 'note']
      const data = list.map(v => filterVal.map(j => { return v[j] }))
      this.$u.exports.excel('短信发送记录', tHeader, data)
    }
  }
}
</script>

<style lang="scss" scoped>

::v-deep{
  .code .el-textarea.is-disabled .el-textarea__inner {
    background-color: #060606 !important;
    color: #c6c6c6 !important;
  }

  .ack .el-input{
    width: 120px;
  }
}

</style>
